import { 
  Controller, 
  Get, 
  Post, 
  Put, 
  Delete, 
  Body, 
  Param, 
  Query, 
  ParseIntPipe
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBearerAuth } from '@nestjs/swagger';
import { Roles, CurrentUser } from '@app/auth';
import { 
  CreateBlogDto, 
  UpdateBlogDto, 
  QueryBlogsDto, 
  BatchBlogActionDto 
} from '../dto';
import { BlogsService } from '../services/blogs.service';

@ApiTags('文章管理')
@ApiBearerAuth()
@Controller('admin/posts')
export class PostsController {
  constructor(private readonly blogsService: BlogsService) {}

  @Get()
  @ApiOperation({ summary: '获取文章列表' })
  @ApiResponse({ status: 200, description: '返回文章列表' })
  @Roles('admin', 'editor')
  async findAll(@Query() query: QueryBlogsDto) {
    const result = await this.blogsService.findAll(query);
    return {
      posts: result.blogs,
      pagination: result.pagination
    };
  }

  @Get(':id')
  @ApiOperation({ summary: '获取文章详情' })
  @ApiParam({ name: 'id', description: '文章ID' })
  @ApiResponse({ status: 200, description: '返回文章详情' })
  @Roles('admin', 'editor')
  async findOne(@Param('id', ParseIntPipe) id: number) {
    return await this.blogsService.findOne(id);
  }

  @Post()
  @ApiOperation({ summary: '创建文章' })
  @ApiResponse({ status: 201, description: '文章创建成功' })
  @Roles('admin', 'editor')
  async create(@Body() createPostDto: CreateBlogDto, @CurrentUser() user: any) {
    if (!createPostDto.authorId && user?.id) {
      createPostDto.authorId = user.id;
    }
    return await this.blogsService.create(createPostDto);
  }

  @Put(':id')
  @ApiOperation({ summary: '更新文章' })
  @ApiParam({ name: 'id', description: '文章ID' })
  @ApiResponse({ status: 200, description: '文章更新成功' })
  @Roles('admin', 'editor')
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updatePostDto: UpdateBlogDto,
    @CurrentUser() user: any
  ) {
    return await this.blogsService.update(id, updatePostDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除文章' })
  @ApiParam({ name: 'id', description: '文章ID' })
  @ApiResponse({ status: 200, description: '文章删除成功' })
  @Roles('admin')
  async remove(@Param('id', ParseIntPipe) id: number) {
    await this.blogsService.remove(id);
    return null;
  }

  @Post('batch')
  @ApiOperation({ summary: '批量操作文章' })
  @ApiResponse({ status: 200, description: '批量操作成功' })
  @Roles('admin', 'editor')
  async batchUpdate(@Body() batchUpdateDto: BatchBlogActionDto) {
    return await this.blogsService.batchAction(batchUpdateDto);
  }

  @Put(':id/publish')
  @ApiOperation({ summary: '发布文章' })
  @ApiParam({ name: 'id', description: '文章ID' })
  @ApiResponse({ status: 200, description: '文章发布成功' })
  @Roles('admin', 'editor')
  async publish(@Param('id', ParseIntPipe) id: number) {
    await this.blogsService.publish(id);
    return null;
  }

  @Put(':id/unpublish')
  @ApiOperation({ summary: '取消发布文章' })
  @ApiParam({ name: 'id', description: '文章ID' })
  @ApiResponse({ status: 200, description: '取消发布成功' })
  @Roles('admin', 'editor')
  async unpublish(@Param('id', ParseIntPipe) id: number) {
    await this.blogsService.unpublish(id);
    return null;
  }

  @Put(':id/top')
  @ApiOperation({ summary: '置顶文章' })
  @ApiParam({ name: 'id', description: '文章ID' })
  @ApiResponse({ status: 200, description: '置顶成功' })
  @Roles('admin', 'editor')
  async setTop(@Param('id', ParseIntPipe) id: number) {
    await this.blogsService.setTop(id);
    return null;
  }

  @Delete(':id/top')
  @ApiOperation({ summary: '取消置顶文章' })
  @ApiParam({ name: 'id', description: '文章ID' })
  @ApiResponse({ status: 200, description: '取消置顶成功' })
  @Roles('admin', 'editor')
  async removeTop(@Param('id', ParseIntPipe) id: number) {
    await this.blogsService.removeTop(id);
    return null;
  }

  @Put(':id/restore')
  @ApiOperation({ summary: '恢复已删除文章' })
  @ApiParam({ name: 'id', description: '文章ID' })
  @ApiResponse({ status: 200, description: '恢复成功' })
  @Roles('admin', 'editor')
  async restore(@Param('id', ParseIntPipe) id: number) {
    await this.blogsService.restore(id);
    return null;
  }
}
